package com.lz.helper.face

import android.util.Log
import java.lang.ref.WeakReference
import java.util.concurrent.atomic.AtomicBoolean

//相机中的人脸检测逻辑
class FaceOperator(
    private val w: Int,
    private val h: Int,
    private val interval: Long,
    private val timeout: Long,
    callback: FaceCallback
) : Runnable {

    companion object {

        const val TAG = "FaceOperator"
    }

    //要处理的数据, 缓存
    private val buffer: ByteArray = ByteArray(w * h * 3 / 2)

    //nv检测数据
    private var nvFeature: FloatArray? = null

    //是否暂停, 暂停时将线程挂起
    private var isPaused = false

    //是否退出
    private val quit = AtomicBoolean(false)

    //是否正在识别
    private var isCoding = false

    //回调
    private val reference = WeakReference(callback)

    //锁
    private val async = Object()

    //匹配请求与匹配时间
    private var request: FeatureRequest? = null
    private var requestTime: Long = 0L

    private var isFeature = false

    //暂停
    fun pause() {
        synchronized(async) { isPaused = true }
    }

    //恢复
    fun restart() {
        synchronized(async) { isPaused = false; async.notify() }
    }

    //中断退出
    fun quit() {
        synchronized(async) { quit.set(true); async.notify() }
    }

    //推入相机中的识别数据
    fun pushNvFeatureData(data: ByteArray, nvFeature: FloatArray) {
        synchronized(async) {
            if (isPaused || isCoding || quit.get()) return
            System.arraycopy(data, 0, buffer, 0, data.size)
            isCoding = true
            this.nvFeature = nvFeature
            Log.d(TAG, "pushNvFeatureData...")
            async.notify()
        }
    }

    private fun synWait() {
        synchronized(async) {
            while (!quit.get() &&(!isCoding || isPaused)) {
                try {
                    async.wait(interval)
                } catch (_: InterruptedException) {
                }
                if (checkRequest()) {
                    setFeature(false)
                }
            }
        }
    }

    @Synchronized
    private fun setFeature(isFeature: Boolean) {
        this.isFeature = isFeature
    }

    @Synchronized
    fun pushFeatureRequest(request: FeatureRequest) {
        if (isFeature) return
        this.request = request
        this.requestTime = System.currentTimeMillis()
    }

    //检测匹配请求是否超时
    @Synchronized
    private fun checkRequest(): Boolean {
        if (request == null) return true
        val currTime = System.currentTimeMillis()
        if (currTime - requestTime > timeout) {
            Log.d(TAG, "checkRequest timeout and setFeature false......")
            request!!.timeout()
            request = null
            return true
        }
        return false
    }

    override fun run() {
        while (!quit.get()) {
            synWait()
            if (quit.get()) break
            setFeature(true)
            if (request != null) {
                /*if(request!!.recognize(buffer, nvFeature) > 0) {
                    request = null
                }
                if (quit.get()) break*/
            }
            if (checkRequest()) {
                setFeature(false)
            }
            synchronized(async) {
                isCoding = false
            }
        }

    }

    interface FaceCallback {

        fun callback(nvData: ByteArray, detectRes: IntArray)
    }
}